import React, {Component} from 'react';
import {View, Text} from 'react-native'
import SafeAreaViewPlus from "../../common/SafeAreaViewPlus";
import { MapView, MapTypes, Geolocation, Overlay, MapApp,Marker } from 'react-native-s-baidumap'
import Utils from "../../utils/Utils";

class Map extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: true,
            location: '',
            // 崇州坐标
            lat: 30.63063,
            lng: 103.698403
        }
    }

     componentDidMount() {
        // this.getLocation()
        // setTimeout(()=>{
        //     this.setState({
        //         loading: false
        //     })
        // }, 1000)
    }
    async getLocation(){
        const data = await Utils.getLocation()
        // {
        //     "addressComponent": {"adcode": "512022", "building": {"name": [Array], "type": [Array]},
        //     "businessAreas": [[Array]], "city": "资阳市", "citycode": "0832", "country": "中国", "district": "乐至县",
        //         "neighborhood": {"name": [Array], "type": [Array]}, "province": "四川省",
        //             "streetNumber": {"direction": "东", "distance": "145.762", "location": "105.013847,30.286682", "number": "59号", "street": "天池大道一段"}, "towncode": "512022001000", "township": "天池街道"}, "formattedAddress": "四川省资阳市乐至县天池街道晶鑫·天池府邸"
        // }
        const {addressComponent} = data
        // console.log(data.addressComponent.streetNumber.location)
        let lat;
        let lng;
        if(addressComponent && addressComponent.streetNumber){
            lng = addressComponent.streetNumber.location.split(',')[0]
            lat = addressComponent.streetNumber.location.split(',')[1]
        }
        this.setState({
            loading: false,
            location: data,
            lat: lat? parseFloat(lat): null,
            lng: lng? parseFloat(lng): null
        })
    }

    componentDidMount() {
        this._navListener = this.props.navigation.addListener('didFocus', () => {
            this.getLocation()

        });
        this._navListener2 = this.props.navigation.addListener('didBlur', () => {
            this.setState({
                loading: true
            })
        });
    }
    componentWillUnmount() {
        this._navListener && this._navListener.remove();
        this._navListener2 && this._navListener2.remove()
    }

    render() {
        const {lng,lat} = this.state
        // console.log(lng,lat)
        return (
            <SafeAreaViewPlus>
                {/*centerLatLng={{latitude: 30.63063,longitude: 103.698403}}*/}
                {this.state.loading?
                    null:
                    <MapView style={{flex: 1}} zoom={12} centerLatLng={{latitude: lat,longitude: lng}} locationEnabled={true}>
                        {/*<Marker location={{ longitude: lng, latitude: lat }} />*/}
                        {/*    <Marker location={{ longitude: 113.968453, latitude: 22.531045 }} />*/}
                        {/*    <Marker location={{ longitude: 113.967453, latitude: 22.532045 }} />*/}
                        {/*    <Marker location={{ longitude: 113.966453, latitude: 22.533045 }} />*/}
                        {/*    <Marker location={{ longitude: 113.965453, latitude: 22.534045 }} />*/}
                        {/*    <Marker location={{ longitude: 113.965453, latitude: 22.535045 }} />*/}
                    </MapView>
                }
            </SafeAreaViewPlus>
        );
    }
}
export default Map;
